package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.common.Common;
import com.yc.jpyy.mobile.dao.AdminDao;
import com.yc.jpyy.mobile.service.AdminService;

@Controller
@RequestMapping(value = "/admin")
public class UpdateAdminPwdController extends BaseController{
	@Autowired
	private AdminService adminService;

	@Autowired
	private AdminDao adminDao;
	private static Logger logger = LoggerFactory.getLogger(UpdateAdminPwdController.class);
	
	/**
	 * 更改管理员密码
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateAdminPwd", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateAdminPwd(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 用户账号
			String account = request.getParameter("account");
			// 原密码
			String oldPwd = request.getParameter("oldPwd");
			// 新密码
			String newsPwd = request.getParameter("newsPwd");
			
			if(account != null && !account.isEmpty()
					&& oldPwd != null && !oldPwd.isEmpty()
					&& newsPwd != null && !newsPwd.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("account", account);
				paramsMap.put("oldPwd", Common.genMDInfo(oldPwd));
				paramsMap.put("newsPwd", Common.genMDInfo(newsPwd));
				//更改密码
				response = adminService.updatePassword(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("updateAdminPwd:"+ e.toString());
			response.setReturnMsg(0,"密码更改失败！");
		} finally {
			return response.toJSON();
		}
	}

	
	

}
